package johnsen.id.project;

import java.awt.*;
import java.awt.event.*;
import johnsen.id.project.StdDraw;

public class PongMain {
	public static final double PADDLEHEIGHT = .19;
	public static final double PADDLEWIDTH = .03;
	public static final int HEIGHT = 500;
	public static final int WIDTH = 800;
	public static final double LEFTPADDLESPEED = 0.3;
	public static final double RIGHTPADDLESPEED = 0.3;

	public static void main(String[] args) {

		StdDraw.setCanvasSize(WIDTH, HEIGHT);
		StdDraw.setXscale(-1.0, 1.0);
		StdDraw.setYscale(-1.0, 1.0);
		game();

	}

	public static void game() {
		Ball ball = new Ball();

		double ballPosX = ball.getBallPosX();
		double ballPosY = ball.getBalltPosY();
		double ballVelY = ball.getBallVelY();
		double ballVelX = ball.getBallVelX();
		double ballRad = ball.getBallRadius();

		double leftPaddlePosY = 0;
		double rightPaddlePosY = 0;

		int pScore = 0;
		int cScore = 0;

		while (true) {
			// Make the ball bouncy! Vectors are fun!
			if (Math.abs(ballPosY) > 1 + ballRad) {
				ballVelY = -ballVelY;
			}
			if (Math.abs(ballPosX) > 1 + ballRad) {
				ballVelX = -ballVelX;
			}

			// Check for collision scoring on player's side
			if (ballPosX + ballVelX - ballRad < -1.1) {
				ballPosX = ballPosY = 0;
				ballVelX = -ballVelX;
				cScore++;
				System.out.println("Computer has " + cScore + " points.");
			}
			
			// Check for collision scoring on computer's side
			if (ballPosX + ballVelX - ballRad > 1.1) {
				ballPosX = ballPosY = 0;
				ballVelX = -ballVelX;
				pScore++;
				System.out.println("Computer has " + pScore + " points.");
			}

			// Check for collision on Player's paddle
			if ((ballPosX - ballRad < -1)
					&& (ballPosY- ballRad > leftPaddlePosY - (PADDLEHEIGHT + .006) )
					&& (ballPosY - ballRad < leftPaddlePosY + (PADDLEHEIGHT + .006))) {
				ballVelX = -ballVelX;

			}
			
			if ((ballPosX - ballRad > .9)
					&& (ballPosY- ballRad > rightPaddlePosY - (PADDLEHEIGHT + .006) )
					&& (ballPosY - ballRad < rightPaddlePosY + (PADDLEHEIGHT + .006))) {
				ballVelX = -ballVelX;

			}
			
			
			// update Ball Position
			ballPosX = ballPosX + ballVelX;
			ballPosY = ballPosY + ballVelY;

			// get user input for the paddle
			if (StdDraw.isKeyPressed(KeyEvent.VK_DOWN)) {
				leftPaddlePosY -= .03;
			}
			if (StdDraw.isKeyPressed(KeyEvent.VK_UP)) {
				leftPaddlePosY += .03;
			}
			
			// Make the computer paddle AI
			if (ballPosY > rightPaddlePosY) {
				rightPaddlePosY += .012;
			}
			if (ballPosY < rightPaddlePosY) {
				rightPaddlePosY -= .012;
			}

			// clear the screen
			clear();
			// render the ball and paddles
			renderBall(ballPosX, ballPosY, .0001, StdDraw.BLACK);
			renderPaddleLeft(-1, leftPaddlePosY, StdDraw.BLACK);
			renderPaddleRight(1, rightPaddlePosY, StdDraw.BLACK);

			// display and pause for 20 ms
			StdDraw.show(15);
		}
	}

	public static void renderBall(double x, double y, double radius, Color color) {
		StdDraw.picture(x, y, "res/ball.png");
		StdDraw.circle(x, y, radius);
	}

	public static void renderPaddleLeft(double x, double y, Color color) {
		StdDraw.picture(x, y, "res/P-paddle.png");
		//StdDraw.setPenColor(StdDraw.RED);
		StdDraw.rectangle(x, y, PADDLEWIDTH, PADDLEHEIGHT);
		

	}
	
	public static void renderPaddleRight(double x, double y, Color color) {
		StdDraw.picture(x, y, "res/C-paddle.png");
		//StdDraw.setPenColor(StdDraw.BLUE);
		StdDraw.rectangle(x, y, PADDLEWIDTH, PADDLEHEIGHT);
	}

	public static void clear() {
		StdDraw.clear(StdDraw.BLACK);
	}

}